//
//  JFAlertAction.m
//  Test
//
//  Created by Jianbo Cai on 2018/8/29.
//  Copyright © 2018年 JF. All rights reserved.
//

#import "FZAlertAction.h"
#import <JFBase/JFBase.h>

@interface FZAlertAction ()

@property (nonatomic, assign) UIAlertActionStyle style;

@end

@implementation FZAlertAction

+ (instancetype)actionWithTitle:(nullable NSString *)title style:(UIAlertActionStyle)style handler:(void (^ __nullable)(FZAlertAction *))handler
{
    FZAlertAction *instance = [FZAlertAction buttonWithType:UIButtonTypeCustom];
    [instance setTitle:title forState:UIControlStateNormal];
    instance.style = style;
    instance.actionHandler = handler;
    instance.enabled = YES; // 默认可用
    return instance;
}

+ (instancetype)actionWithImage:(nullable UIImage *)image style:(UIAlertActionStyle)style handler:(void (^ __nullable)(FZAlertAction *action))handler {
    FZAlertAction *instance = [FZAlertAction buttonWithType:UIButtonTypeCustom];
    [instance setImage:image forState:UIControlStateNormal];
    instance.style = style;
    instance.actionHandler = handler;
    instance.enabled = YES; // 默认可用
    return instance;
}

- (void)dealloc
{
    
}

- (void)setStyle:(UIAlertActionStyle)style {
    _style = style;
    
    self.titleLabel.font = [UIFont fontWithName:@"PingFang SC" size:19];
    
    self.layer.cornerRadius = 22;
    switch (self.style) {
        case UIAlertActionStyleDefault:
            [self setTitleColor:UIColorFromHex(0xFFFFFF) forState:UIControlStateNormal];
            self.backgroundColor = UIColorFromHex(0x5D6C5A);
            break;
        case UIAlertActionStyleCancel:
            [self setTitleColor:UIColorFromHex(0xFC724C) forState:UIControlStateNormal];
            break;
        case UIAlertActionStyleDestructive:
            [self setTitleColor:AWThemeColorForKey(@"Kit.alertActionDisabledColor") forState:UIControlStateNormal];
            break;
            
        default:
            break;
    }
}


- (void)setEnabled:(BOOL)enabled {
    [super setEnabled:enabled];
    if (_style == UIAlertActionStyleDefault) {
        if (enabled) {
            [self setTitleColor:UIColorFromHex(0xFFFFFF) forState:UIControlStateNormal];
            self.backgroundColor = UIColorFromHex(0x5D6C5A);
        }else {
            UIColor *color = AWThemeColorForKey(@"Kit.alertActionDisabledColor");
            self.layer.borderColor = color.CGColor;
            [self setTitleColor:color forState:UIControlStateNormal];
            self.backgroundColor = [UIColor clearColor];
        }
    }
}

@end
